public class Main implements Runnable {
    public static void main(String[] args) {
        Runnable runnable = new Main();
        new Thread(runnable,"乌龟").start();
        new Thread(runnable,"兔子").start();

    }
    //定义胜利者
    private static String Winner;

    @Override
    public void run() {

        for(int i = 1; i <= 100; i++){
            if(Thread.currentThread().getName() == "乌龟"){
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            //判断是否结束
            Boolean flag = isFinished(i);
            if(flag)
                break;
            System.out.println(Thread.currentThread().getName()+"---->跑了"+i+"步");
        }
    }
    //判断是否完成比赛
    private Boolean isFinished(int steps){
        if(Winner != null){
            return true;
        }
        if(steps >= 100){
            Winner = Thread.currentThread().getName();
            System.out.println("Winner is "+Winner);
            return true;
        }
        return false;
    }

}
